package mbsl.main;

import mbsl.ui.ImageLoader;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class LoadingScreen {
	private ProgressBar mProgressBar;
	private Shell mShell;

	public LoadingScreen(Display pDisplay) {
		mShell = new Shell(pDisplay, SWT.ON_TOP);

		mShell.setLayout(new FormLayout());
		
		Image image = ImageLoader.getImage(pDisplay, "splashLogo");
		mProgressBar = new ProgressBar(mShell, SWT.NONE);
		Label imageLabel = new Label(mShell, SWT.NONE);

		FormData imageLabelData = new FormData();
		
		imageLabelData.top = new FormAttachment(0, 0);
		imageLabelData.right = new FormAttachment(100, 0);
		imageLabelData.left = new FormAttachment(0, 0);
		
		imageLabel.setImage(image);
		imageLabel.setLayoutData(imageLabelData);
		imageLabel.pack();

		FormData progressBarData = new FormData();
		
		progressBarData.top = new FormAttachment(imageLabel, 0);
		progressBarData.right = new FormAttachment(100, 0);
		progressBarData.left = new FormAttachment(0, 0);
		
		mProgressBar.setLayoutData(progressBarData);
		mProgressBar.setMaximum(100);
		
		mShell.pack();
		
		mShell.setLocation(getLocation());
	}
	
	public void setValue(int pValue) {
		mProgressBar.setSelection(pValue);
	}
	public void open() {
		mShell.open();
	}
	public void close() {
		mShell.dispose();
	}
	
	private Point getLocation() {
		Display display = mShell.getDisplay();
		
		Point size = mShell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		Rectangle monitor = display.getPrimaryMonitor().getClientArea();

		int x = monitor.x + monitor.width / 2 - size.x / 2;
		int y = monitor.y + monitor.height / 2 - size.y / 2;
		
		return new Point(x, y);
	}
}
